package io.renren.modules.noc.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.noc.entity.NocMemberEntity;
import io.renren.modules.noc.entity.NocTemplateEntity;
import io.renren.modules.noc.service.NocMemberService;
import io.renren.modules.noc.service.NocTemplateService;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.noc.entity.NocRepayEntity;
import io.renren.modules.noc.service.NocRepayService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author whc
 * @email hucheng_86@126.com
 * @date 2019-06-28 18:04:28
 */
@RestController
@RequestMapping("noc/nocrepay")
public class NocRepayController extends BaseController{
    @Autowired
    private NocRepayService nocRepayService;
    @Autowired
    private NocMemberService nocMemberService;
    @Autowired
    private NocTemplateService nocTemplateService;

    @Value("${ad.short.url}")
    private String shortUrl;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("noc:nocrepay:list")
    public R list(@RequestParam Map<String, Object> params){
        String current = (String)params.get("page");
        String limit = (String)params.get("limit");
        Page<Map<String, Object>> page = new Page<Map<String, Object>>(Integer.parseInt(current), Integer.parseInt(limit));
        List<NocMemberEntity> result = nocMemberService.getRepay(page,params);
        //分页
        String total = page.getTotal()+"";
        String size = page.getSize()+"";
        String curr = page.getCurrent()+"";
        PageUtils page2 = new PageUtils(result,Integer.parseInt(total),Integer.parseInt(size),Integer.parseInt(curr));
        return R.ok().put("page", page2);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("noc:nocrepay:info")
    public R info(@PathVariable("id") Integer id){
        //NocRepayEntity nocRepay = nocRepayService.getById(id);
        NocMemberEntity nocMember =  nocMemberService.getById(id);

        return R.ok().put("nocMember", nocMember);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("noc:nocrepay:save")
    public R save(@RequestBody NocRepayEntity nocRepay){
        nocRepayService.save(nocRepay);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("noc:nocrepay:update")
    public R update(@RequestBody NocMemberEntity nocMember){
        ValidatorUtils.validateEntity(nocMember);
        SysUserEntity user = this.getUser();
        nocMember.setUpdator(user.getRemark());
        nocMember.setUpdateTime(new Date());
        nocMemberService.updateById(nocMember);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("noc:nocrepay:delete")
    public R delete(@RequestBody Integer[] ids){
        nocRepayService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 短信推送电子门票
     * @param id
     * @return
     */
    @RequestMapping("/sendmsg/{id}")
    @RequiresPermissions("noc:nocrepay:msg")
    public R sendmsg(@PathVariable("id") Integer id,HttpServletRequest req) {
        NocMemberEntity member = nocMemberService.getById(id);
        String tid = member.getTid() + "";
        NocTemplateEntity template = nocTemplateService.getById(tid);
        //链接处理
        StringBuffer url = req.getRequestURL();
        String shortUrl = url.delete(url.length() - req.getRequestURI().length(), url.length()).append("/").toString();
        shortUrl = shortUrl.replace("http://", "").replace("https://", "");
        String _url = shortUrl + "t/" + member.getId();
        String content = "尊敬的" + member.getCol3() + ",您" + template.getTemplate() + "的预登记电子门票链接 " + _url + " ,请直接现场刷闸机入场";
        this.sendContentMsg(tid, member.getCol1(), content);
        return R.ok();
    }

}
